package com.packtpub.e4.clock.ui.views;

import java.net.URL;
import java.util.TimeZone;

import org.eclipse.jface.resource.FontRegistry;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.resource.LocalResourceManager;
import org.eclipse.jface.resource.ResourceManager;
import org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.part.ViewPart;

import com.packtpub.e4.clock.ui.internal.TimeZoneComparator;
import com.packtpub.e4.clock.ui.internal.TimeZoneDialog;
import com.packtpub.e4.clock.ui.internal.TimeZoneViewerComparator;
import com.packtpub.e4.clock.ui.internal.TimeZoneViewerFilter;

public class TimeZoneTreeView extends ViewPart {
    private TreeViewer treeViewer;

    public TimeZoneTreeView() {
    }

    @Override
    public void createPartControl(Composite parent) {
        ResourceManager rm = JFaceResources.getResources();
        LocalResourceManager lrm = new LocalResourceManager(rm);

        ImageRegistry ir = new ImageRegistry(lrm);
        URL sample = getClass().getResource("/icons/sample.gif");
        ir.put("sample", ImageDescriptor.createFromURL(sample));

        FontRegistry fr = JFaceResources.getFontRegistry();

        treeViewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
        treeViewer.setLabelProvider(new DelegatingStyledCellLabelProvider(new TimeZoneLabelProvider(ir, fr)));
        treeViewer.setContentProvider(new TimeZoneContentProvider());
        treeViewer.setData("REVERSE", Boolean.TRUE);    // Viewer에 사용자 정의 값을 지정
        treeViewer.setComparator(new TimeZoneViewerComparator());
        treeViewer.setFilters(new ViewerFilter[] {new TimeZoneViewerFilter("GMT")});
        treeViewer.setExpandPreCheckFilters(true);  // Expanded 여부를 체크하여 아이콘 변경
        treeViewer.setInput(new Object[] {TimeZoneComparator.getTimeZones()});
//        treeViewer.expandAll();

        treeViewer.addDoubleClickListener(new IDoubleClickListener() {
            @Override
            public void doubleClick(DoubleClickEvent event) {
                Viewer viewer = event.getViewer();
                Shell shell = viewer.getControl().getShell();
                ISelection sel = viewer.getSelection();
                Object selectedValue;
                if (!(sel instanceof IStructuredSelection) || sel.isEmpty()) {
                    selectedValue = null;
                } else {
                    selectedValue = ((IStructuredSelection) sel).getFirstElement();
                    if (selectedValue instanceof TimeZone) {
                        TimeZone timeZone = (TimeZone) selectedValue;
                        new TimeZoneDialog(shell, timeZone).open();
                    }
                }
            }
        });
    }

    @Override
    public void setFocus() {
        treeViewer.getControl().setFocus();
    }
}
